package com.example.petmall.controller;

import com.example.petmall.entity.Pet;
import com.example.petmall.repository.PetRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class PetController {
    @Autowired
    private PetRepository petRepository;

    @GetMapping("/pets")
    public List<Pet> getAllPets() {
        return petRepository.findAll();
    }

    @GetMapping("/pets/{id}")
    public Pet getPet(@PathVariable Integer id) {
        return petRepository.findById(id).orElse(null);
    }

    @PostMapping("/pets")
    public Pet addPet(@RequestBody Pet pet) {
        return petRepository.save(pet);
    }
}    